﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BusinessClient;
using Common; 

namespace AdminizzaUIDesktop.form
{
    public partial class ClientModule : AdminizzaUIDesktop.UIForm
    {
        private BizClient biz;
        Client clients;
        private DataTable personas;
        private DataTable telefonos;
        private DataTable direcciones;

        private int personItem;
        private int addressItem;
        private int phoneItem;

        public ClientModule():base()
        {
            InitializeComponent();
        }

        private void ClientModule_Load(object sender, EventArgs e)
        {
            try
            {
                biz = new BizClient(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Adminizza\AdminizzaSandBox\Common\AdminizzaDB.mdf;Integrated Security=True;User Instance=True");
                clients = biz.GetClients();
                this.personas = clients.Personas;
                this.telefonos = clients.Telefonos;
                this.direcciones = clients.Direcciones;
                this.LoadData();
                this.tssState.Text = "Datos Cargados.";
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
                this.tssState.Text = "Error al cargar datos.";
            }
        }

        private void LoadData()
        {
            lstPersons.DataSource = this.personas;
            lstPersons.DisplayMember = "NyA";
            txtIdClient.DataBindings.Add("Text",personas,"ClienteId");
            txtPerson.DataBindings.Add("Text", personas, "NyA");
            txtIdPerson.DataBindings.Add("Text", personas, "Id");
        }

        private void lstPersons_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnGetPersonData.Enabled = true;
            
        }
        private void btnGetPersonData_Click(object sender, EventArgs e)
        {
            btnGetPersonData.Enabled = false;
            //Filtro por cliente:
            this.direcciones.DefaultView.RowFilter = string.Format("ClienteFk = {0}", txtIdClient.Text) ;
            lstAddresses.DataSource = this.direcciones;
            lstAddresses.DisplayMember = "Data";
            txtAddress.DataBindings.Clear();
            txtAddress.DataBindings.Add("Text", direcciones, "Data");

            //Filtro por cliente:
            this.telefonos.DefaultView.RowFilter = string.Format("ClienteFk = {0}", txtIdClient.Text);
            lstPhones.DataSource = this.telefonos;
            lstPhones.DisplayMember = "Data";
            txtPhone.DataBindings.Clear();
            txtPhone.DataBindings.Add("Text", telefonos, "Data");

        }
        //TODO:
        private void btnDelAddress_Click(object sender, EventArgs e)
        {
            
        }
        //TODO:
        private void btnDelPhone_Click(object sender, EventArgs e)
        {
            
        }
        //TODO:
        private void btnAddAddress_Click(object sender, EventArgs e)
        {
            if (txtAddress.Text.Length > 0)
            {

            }
        }
        //TODO:
        private void btnAddPhone_Click(object sender, EventArgs e)
        {
            if (txtPhone.Text.Length > 0)
            {

            }
        }
        //TODO: Impacta los cambios
        private void bntSave_Click(object sender, EventArgs e)
        {
            //?biz.InsertClient(this.clients);
        }

    }
}